﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Interfaces;
using OpenNETCF.Drawing.Imaging;
using System.Drawing;

namespace PozrSvc
{
    public class Markers
    {
        private List<Marker> markerList;
        private List<Prototype> protoList;

        public class Prototype
        {
            private static int proto_id_counter = 0;

            public int Id { get; private set; }
            public System.IO.Stream BackgroundImage { get; private set; }
            public System.Drawing.Point ImageOffset { get; private set; }
            public System.Drawing.Point Offset { get; private set; }
            public Font Font { get; set; }
            public Brush FontBrush { get; set; } 
            public string Owner { get; private set; }

            public Prototype(System.IO.Stream bgImage, System.Drawing.Point imageOffset, System.Drawing.Point textOffset, string owner)
            {
                this.Id = proto_id_counter++;
                this.BackgroundImage = bgImage;
                this.ImageOffset = imageOffset;
                this.Offset = textOffset;
                this.Owner = owner;
            }
        }

        public class Marker
        {
            private static int id_counter = 0;

            public int Id { get; private set; }
            public double Latitude { get; set; }
            public double Longitude { get; set; }
            public int ProtoId { get; private set; }

            public static int CurrentId { get { return id_counter; } }

            public Marker(double _latitude, double _longitude, int proto_id)
            {
                this.Id = id_counter++;
                this.Latitude = _latitude;
                this.Longitude = _longitude;
                this.ProtoId = proto_id;
            }
        }

        public Markers()
        {
            markerList = new List<Marker>();
            protoList = new List<Prototype>();
        }

        public List<Marker> MarkerList
        {
            get { return markerList; }
        }

        public List<Prototype> ProtoList
        {
            get { return protoList; }
        }

        public int Add(double lat, double lon, int proto_id, string text, Bitmap image)
        {
            Marker marker = new Marker(lat, lon, proto_id);
            markerList.Add(marker);
            
      /*      Map.MarkerDescriptor mdsc = new Map.MarkerDescriptor();
            mdsc.BgImageStream = protoList[proto_id].BackgroundImage;
            mdsc.Offset = protoList[proto_id].Offset;
            mdsc.ImageOffset = protoList[proto_id].ImageOffset;

            if (text != null)
            {
                mdsc.Text = text;
                mdsc.Font = protoList[proto_id].Font;
            }

            if (image != null)
            {
                mdsc.Bitmap = image;
            }

            Middleware.MapDisplay.Map.AddMarker(marker.Latitude, marker.Longitude, mdsc);*/

            Middleware.OnLogEvent("Marker " + marker.Id.ToString() + " added @ " + lat.ToString() + ", " + lon.ToString());

            return marker.Id;
        }

        public bool Move(int marker_id, double latitude, double longitude)
        {
            foreach (Marker m in markerList)
            {
                if (m.Id == marker_id)
                {
                    m.Longitude = longitude;
                    m.Latitude = latitude;

                    Middleware.OnLogEvent("Marker " + marker_id.ToString() + " moved to " + latitude.ToString() + ", " + longitude.ToString());

                    return true;
                }
            }
            return false;
        }

        public int AddProto(System.IO.Stream bgImage, System.Drawing.Point imageOffset, System.Drawing.Point offset, string owner)
        {
            Prototype proto = new Prototype(bgImage, imageOffset, offset, owner);
            protoList.Add(proto);
            return proto.Id;
        }
    }
}
